home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CPPTASK.ARJ / TSKTIMER.CPP < prev    next >
C/C++ Source or Header  |  1991-08-21  |  3KB  |  152 lines

  1. /*
  2.    CPPTask - A Multitasking Kernel For C++
  3.  
  4.    Version 1.0 08-12-91
  5.  
  6.    Ported by Rich Smith from:
  7.  
  8.    Public Domain Software written by
  9.       Thomas Wagner
  10.       Patschkauer Weg 31
  11.       D-1000 Berlin 33
  12.       West Germany
  13.  
  14.    TSKTIMER.CPP - Timer routines
  15.  
  16.    Subroutines:
  17.         timer::timer
  18.         timer::~timer
  19.         timer::change_timer
  20.         timer::tsk_timer_action
  21. */
  22.  
  23. #include "task.hpp"
  24. #include "tsklocal.hpp"
  25.  
  26. /*
  27.    timer
  28.       Creates a timer queue element. The element is inserted into
  29.       the timeout queue.
  30. */
  31.  
  32. timer::timer (dword tout, farptr structp, byte kind, byte rept)
  33. {
  34.    CRITICAL;
  35.  
  36.    tkind = kind;
  37.    strucp = structp;
  38.    timeout = reload = tsk_timeout(tout);
  39.  
  40.    if (kind <= TKIND_TASK || kind > TKIND_COUNTER)
  41.       {
  42.       tstate = TSTAT_IDLE;
  43.       next = NULL;
  44.       }
  45.    else
  46.       {
  47.       tstate = (byte)((rept) ? TSTAT_REPEAT : TSTAT_COUNTDOWN);
  48.  
  49.       C_ENTER;
  50.       next = tsk_timer;
  51.       tsk_timer = this;
  52.       C_LEAVE;
  53.       }
  54. }
  55.  
  56.  
  57. /*
  58.    ~timer
  59.       Deletes a timeout element.
  60. */
  61.  
  62. timer::~timer ()
  63. {
  64.    CRITICAL;
  65.  
  66.    C_ENTER;
  67.    if (tstate != TSTAT_IDLE)
  68.       {
  69.       tstate = (byte)TSTAT_REMOVE;
  70.       C_LEAVE;
  71.       return;
  72.       }
  73.    C_LEAVE;
  74.  
  75. }
  76.  
  77.  
  78. /*
  79.    change_timer
  80.       Changes the timeout and/or repeat-flag in a timer element.
  81.       If the timer was idle, it is inserted into the timeout queue.
  82.  
  83.       If 0 is passed as timeout, the element is removed from the
  84.       timeout queue (same as ~timer).
  85.  
  86.       This routine should *not* be used for dynamically allocated
  87.       timer elements.
  88. */
  89.  
  90. void far timer::change_timer (dword tout, byte rept)
  91. {
  92.    byte st;
  93.    CRITICAL;
  94.  
  95.    if (!tout)
  96.       {
  97.       this->timer::~timer();
  98.       return;
  99.       }
  100.  
  101.    C_ENTER;
  102.    timeout = reload = tsk_timeout(tout);
  103.    st = tstate;
  104.    tstate = (byte)((rept) ? TSTAT_REPEAT : TSTAT_COUNTDOWN);
  105.  
  106.    if (st == TSTAT_IDLE)
  107.       {
  108.       next = tsk_timer;
  109.       tsk_timer = this;
  110.       }
  111.    C_LEAVE;
  112. }
  113.  
  114.  
  115.  
  116. /*
  117.    tsk_timer_action performs the necessary action when a timeout occurred.
  118. */
  119.  
  120. void timer::tsk_timer_action (void)
  121. {
  122.    tcbptr taskptr;
  123.    byte st;
  124.  
  125.    switch (tkind & 0x7f)
  126.       {
  127.       case TKIND_WAKE:
  128.       case TKIND_TASK:  taskptr = (tcbptr) strucp;
  129.                         st = taskptr->get_state();
  130.  
  131.                         if (st == ST_WAITING || st == ST_DELAYED)
  132.                            {
  133.                            taskptr->set_retptr(TTIMEOUT);
  134.                            taskptr->wake_task ();
  135.                            }
  136.                         break;
  137.  
  138.       case TKIND_FLAG:  ((flagptr) strucp)->set_flag ();
  139.                         break;
  140.  
  141.       case TKIND_COUNTER: ((counterptr) strucp)->inc_counter ();
  142.                         break;
  143.  
  144.       case TKIND_PROC:  ((funcptr) strucp)();
  145.                         break;
  146.  
  147.       default:          break;
  148.       }
  149. }
  150.  
  151.  
  152.